package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.mapper.ClassesMapper;
import com.itheima.pojo.Classes;
import com.itheima.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * The type Classes service.
 */
@Service
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public void addClass(Classes classes) {
        if (classes.getName().length() > 30 || classes.getName().length() < 4) {
            throw new RuntimeException("班级名称非法");
        }
        if (classes.getClassesNumber().length() > 20 || classes.getClassesNumber().length() < 1) {
            throw new RuntimeException("教室名称非法");
        }

        // 通过班级名称查询班级名称是否重复
        Integer count = classesMapper.selectCountByName(classes.getName());
        if (count > 0) {
            throw new RuntimeException("班级名称重复");
        }
        if (classes.getClassesNumber() == null || Objects.equals(classes.getClassesNumber(), "0")) {
            throw new RuntimeException("教室不能为空");
        }

        classes.setCreateTime(LocalDateTime.now());
        classes.setUpdateTime(LocalDateTime.now());
        classesMapper.addClass(classes);
    }

    @Override
    public PageBean<Classes> selectClassesByPage(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        PageBean<Classes> pageBean = new PageBean<>();
        PageHelper.startPage(page, pageSize);
        List<Classes> classesList = classesMapper.selectClassesByPage(name, begin, end);
        // 类型转换
        Page<Classes> classesPage = (Page<Classes>) classesList;
        long total = classesPage.getTotal();
        List<Classes> result = classesPage.getResult();
        pageBean.setTotal(total);
        pageBean.setRows(result);
        return pageBean;
    }

    @Override
    public void deleteClassById(Integer id) {
        classesMapper.deleteClassById(id);
    }

    @Override
    public Classes selectClassById(Integer id) {
        return classesMapper.selectClassById(id);
    }

    @Override
    public void updateClassById(Classes classes) {
        classes.setUpdateTime(LocalDateTime.now());

        if (classes.getName().length() > 30 || classes.getName().length() < 4) {
            throw new RuntimeException("班级名称非法");
        }
        if (classes.getClassesNumber().length() > 20 || classes.getClassesNumber().length() < 1) {
            throw new RuntimeException("教室名称非法");
        }
        if (classes.getClassesNumber() == null || Objects.equals(classes.getClassesNumber(), "0")) {
            throw new RuntimeException("教室不能为空");
        }
        // 通过班级名称查询班级名称是否重复并且排除自身的干扰
        Integer count = classesMapper.selectCountByNameNotId(classes.getName(),classes.getId());
        if (count > 0 ) {
            throw new RuntimeException("班级名称已存在");
        }

        classesMapper.updateClassById(classes);
    }
}
